package com.gosophia.conference.action.notification;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.confPreparation.dto.ParticipatingNameListDTO;
import com.gosophia.conferenceManagement.confPreparation.ws.ConfPreparationWebService;
import com.gosophia.conferenceManagement.notification.dto.SendNotificationJobDataDTO;
import com.gosophia.conferenceManagement.notification.ws.SendNotificationWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserAccountDTO;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 获取通知反馈Action，此Action用于邮件链接中的刷反馈页面或者反馈成功页面
 * @author nana
 * @since  2011-3-26
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@SuppressWarnings("serial")
@Scope("prototype")
@Controller("GetNotificationFeedBackAction")
public class GetNotificationFeedBackAction extends ActionSupport {

	private Long jobId;
	
	private Long userId;

	private SendNotificationJobDataDTO sendNotificationJobDataDTO;
	
	private UserAccountDTO userAccountDTO;
	
    private JobDTO jobDto;
    
    private UserInfoDTO uerInfoDTO;

	private String ifAttendString;

	@Autowired
	private SendNotificationWebService sendNotificationWebService;
	
	@Autowired
	private ConfPreparationWebService confPreparationWebService;
	
	@Autowired
	private TaskManagementWebService taskManagementWebService;
	
	@Autowired
	private UserManagementWebService userManagementWebService;

	@Override
	public String execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
        .getContext().getAuthentication().getPrincipal();
		if(userId!=null) {
			if(!userDetails.getUserAccountDTO().getUserInfoDTO().getUserInfoId().equals(userId)) {
	            throw new BusinessException("请您用正确的账号登陆,来进行反馈", "JC2_OA_NF_01");
			}
		}
		
		JobDTO job = taskManagementWebService.findJobDetailByJobId(jobId);
		JobDTO jobMeet = taskManagementWebService.findJobByMissionIdAndJobType(job.getMissionDTO().getMissionId(), "会务准备");
		boolean ifFeedBacked = confPreparationWebService.ifFeedBacked(jobMeet.getJobId(), userId);
		userAccountDTO = userManagementWebService.findUserAccountByUserInfoId(userId);
		sendNotificationJobDataDTO = sendNotificationWebService
		.findSendNotificationJobDataByJobIdAndSendUserId(jobId);
		jobDto = taskManagementWebService.findJobDetailByJobId(sendNotificationJobDataDTO.getJobId());
		List<UserInfoDTO> userInfos = userManagementWebService.findAllUserInfo();
		for(UserInfoDTO userInfo:userInfos) {
			if(userInfo.getUserInfoId().equals(userId)) {
				uerInfoDTO = userInfo;
			}
		}
		
		if(ifFeedBacked){
		

			if(userAccountDTO == null){
				List<ParticipatingNameListDTO> particiNameList =  confPreparationWebService.findNotificationFeedbackListsByJobId(jobMeet.getJobId());
				for(ParticipatingNameListDTO pName:particiNameList) {
					if(pName.getUserName().equals(uerInfoDTO.getUserName())) { 
						if("是".equals(pName.getIfAttend())) {
							ifAttendString = "同意";
						} else {
							ifAttendString = "不同意";
						}
					}
				}
				return "systemOutSuccess";
			}else{
				List<ParticipatingNameListDTO> particiNameList =  confPreparationWebService.findNotificationFeedbackListsByJobId(jobMeet.getJobId());
				for(ParticipatingNameListDTO pName:particiNameList) {
					if(pName.getUserName().equals(userAccountDTO.getUserInfoDTO().getUserName())) { 
						if("是".equals(pName.getIfAttend())) {
							ifAttendString = "同意";
						} else {
							ifAttendString = "不同意";
						}
					}
				}
				return "systemInSuccess";
			}
		}
		if(userAccountDTO == null){
			return "systemOutPage";
		}else{
			return "systemInPage";
		}
	}

	public SendNotificationJobDataDTO getSendNotificationJobDataDTO() {
		return sendNotificationJobDataDTO;
	}

	public void setSendNotificationJobDataDTO(
			SendNotificationJobDataDTO sendNotificationJobDataDTO) {
		this.sendNotificationJobDataDTO = sendNotificationJobDataDTO;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public Long getJobId() {
		return jobId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public UserAccountDTO getUserAccountDTO() {
		return userAccountDTO;
	}

	public void setUserAccountDTO(UserAccountDTO userAccountDTO) {
		this.userAccountDTO = userAccountDTO;
	}

	public JobDTO getJobDto() {
		return jobDto;
	}

	public void setJobDto(JobDTO jobDto) {
		this.jobDto = jobDto;
	}

	public UserInfoDTO getUerInfoDTO() {
		return uerInfoDTO;
	}

	public void setUerInfoDTO(UserInfoDTO uerInfoDTO) {
		this.uerInfoDTO = uerInfoDTO;
	}

	public String getIfAttendString() {
		return ifAttendString;
	}

	public void setIfAttendString(String ifAttendString) {
		this.ifAttendString = ifAttendString;
	}
	
	

}
